package thread;

import java.util.concurrent.*;

/**
 * Created by fanzhenyu02 on 2020/9/4.
 */
public class ThreadCooperation {
    static final int COUNT = 20;

    public static void sleep(int sec) {
        try {
            Thread.sleep(sec * 1000L);
        } catch (InterruptedException e) {
            System.out.println(e);
        }
    }

    public static void println(String msg) {
        System.out.println(msg);
    }

    public static void println(String msg, int num) {
        System.out.printf(msg, num);
        System.out.println("");
    }

    public static void doingLongTime() {
        System.out.println("--- do something.");
    }

    public static void main_01(String[] args) throws Exception {
        new Thread(new Teacher(countDownLatch)).start();
        sleep(1);
        for (int i = 0; i < COUNT; i++) {
            new Thread(new Student(i, countDownLatch)).start();
        }
        synchronized (ThreadCooperation.class) {
            ThreadCooperation.class.wait();
        }
    }

    static CountDownLatch countDownLatch = new CountDownLatch(COUNT);

    static class Teacher implements Runnable {

        CountDownLatch cdl;

        Teacher(CountDownLatch cdl) {
            this.cdl = cdl;
        }

        @Override
        public void run() {
            println("老师发卷子。。。");
            try {
                cdl.await();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            println("老师收卷子。。。");
        }

    }

    static class Student implements Runnable {

        CountDownLatch cdl;
        int num;

        Student(int num, CountDownLatch cdl) {
            this.num = num;
            this.cdl = cdl;
        }

        @Override
        public void run() {
            println("学生(%d)写卷子。。。", num);
            doingLongTime();
            println("学生(%d)交卷子。。。", num);
            cdl.countDown();
        }

    }


    static final int COUNT_01 = 5;

    public static void main(String[] args) throws Exception {
        for (int i = 0; i < COUNT_01; i++) {
            new Thread(new Staff(i, cb)).start();
        }
        synchronized (ThreadCooperation.class) {
            ThreadCooperation.class.wait();
        }
    }

    static CyclicBarrier cb = new CyclicBarrier(COUNT_01, new Singer());

    static class Singer implements Runnable {

        @Override
        public void run() {
            println("为大家唱歌。。。");
        }

    }

    static class Staff implements Runnable {

        CyclicBarrier cb;
        int num;

        Staff(int num, CyclicBarrier cb) {
            this.num = num;
            this.cb = cb;
        }

        @Override
        public void run() {
            println("员工(%d)出发。。。", num);
            doingLongTime();
            println("员工(%d)到达地点一。。。", num);
            try {
                cb.await();
            } catch (Exception e) {
                e.printStackTrace();
            }
            println("员工(%d)再出发。。。", num);
            doingLongTime();
            println("员工(%d)到达地点二。。。", num);
            try {
                cb.await();
            } catch (Exception e) {
                e.printStackTrace();
            }
            println("员工(%d)再出发。。。", num);
            doingLongTime();
            println("员工(%d)到达地点三。。。", num);
            try {
                cb.await();
            } catch (Exception e) {
                e.printStackTrace();
            }
            println("员工(%d)结束。。。", num);
        }

    }


}
